package com.speridian.oms.view.beans.backing;

import com.speridian.oms.view.beans.utils.JSFUtils;
import com.speridian.oms.view.beans.utils.MessageUtils;

import java.math.BigDecimal;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeConstants;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

import javax.xml.ws.WebServiceRef;



import vis.POVendorInsertion_Service;

import vis.types.ObjectFactory;
import vis.types.OmPoVendors;
import vis.types.OmPoVendorsCollection;
import vis.types.POVendorResponse;

public class VendorInsertionBean {
    private String vendorName;
    private String VendorSiteLocation;
    private String BankAccountName;
    private String VendorContactNumber;
    private String BankAccountNumber;
    private String InvoiceCurrencyCode;
    private String CreatedBy;
    private String CreatedDate;
    
        @WebServiceRef
    private static POVendorInsertion_Service pOVendorInsertion_Service;
    public VendorInsertionBean() {
        super();
    }

    public void setVendorName(String vendorName) {
        this.vendorName = vendorName;
    }

    public String getVendorName() {
        return vendorName;
    }

    public void setVendorSiteLocation(String VendorSiteLocation) {
        this.VendorSiteLocation = VendorSiteLocation;
    }

    public String getVendorSiteLocation() {
        return VendorSiteLocation;
    }

    public void setBankAccountName(String BankAccountName) {
        this.BankAccountName = BankAccountName;
    }

    public String getBankAccountName() {
        return BankAccountName;
    }

    public void setVendorContactNumber(String VendorContactNumber) {
        this.VendorContactNumber = VendorContactNumber;
    }

    public String getVendorContactNumber() {
        return VendorContactNumber;
    }

    public void setBankAccountNumber(String BankAccountNumber) {
        this.BankAccountNumber = BankAccountNumber;
    }

    public String getBankAccountNumber() {
        return BankAccountNumber;
    }

    public void setInvoiceCurrencyCode(String InvoiceCurrencyCode) {
        this.InvoiceCurrencyCode = InvoiceCurrencyCode;
    }

    public String getInvoiceCurrencyCode() {
        return InvoiceCurrencyCode;
    }

    public void setCreatedBy(String CreatedBy) {
        this.CreatedBy = CreatedBy;
    }

    public String getCreatedBy() {
        return CreatedBy;
    }

    public void setCreatedDate(String CreatedDate) {
        this.CreatedDate = CreatedDate;
    }

    public String getCreatedDate() {
        return CreatedDate;
    }
    public String passInput() throws ParseException,
                                     DatatypeConfigurationException {


        pOVendorInsertion_Service = new POVendorInsertion_Service();
        OmPoVendorsCollection coll = new OmPoVendorsCollection();
        OmPoVendors obj = new OmPoVendors();
        ObjectFactory oo = new ObjectFactory();


        obj.setBankAccountName(oo.createOmPoVendorsBankAccountName(getBankAccountName()));
        Object created_By =
            JSFUtils.resolveExpression("#{sessionScope.UserSession.userFullname}");
        obj.setCreatedBy(oo.createOmPoVendorsCreatedBy(created_By.toString()));
        Date dob = null;
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        dob = df.parse(getCreatedDate());
        GregorianCalendar cal = new GregorianCalendar();
        cal.setTime(dob);
        XMLGregorianCalendar xmlDate2 =
            DatatypeFactory.newInstance().newXMLGregorianCalendar(cal.get(Calendar.YEAR),
                                                                  cal.get(Calendar.MONTH) +
                                                                  1,
                                                                  cal.get(Calendar.DAY_OF_MONTH),
                                                                  cal.get(Calendar.HOUR_OF_DAY),
                                                                  cal.get(Calendar.MINUTE),
                                                                  cal.get(Calendar.SECOND),
                                                                  DatatypeConstants.FIELD_UNDEFINED,
                                                                  DatatypeConstants.FIELD_UNDEFINED);
        //XMLGregorianCalendar xmlDate2 = DatatypeFactory.newInstance().newXMLGregorianCalendar(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH)+1, cal.get(Calendar.DAY_OF_MONTH),cal.get(Calendar.HOUR_OF_DAY),cal.get(Calendar.MINUTE),DatatypeConstants.FIELD_UNDEFINED, cal.getTimeZone().LONG).normalize();
        obj.setCreatedDate(oo.createOmPoVendorsCreatedDate(xmlDate2));

        //obj.setCreatedDate(xmlDate2);
        obj.setInvoiceCurrencyCode(oo.createOmPoVendorsInvoiceCurrencyCode(getInvoiceCurrencyCode()));
        obj.setVendorContact(oo.createOmPoVendorsVendorContact(getVendorContactNumber().toString()));
        //obj.setVendorId(vendorID);


        obj.setVendorName(oo.createOmPoVendorsVendorName(getVendorName()));
        obj.setVendorSiteLocation(oo.createOmPoVendorsVendorSiteLocation(getVendorSiteLocation()));
        BigDecimal bd = new BigDecimal(getBankAccountNumber().toString());
        obj.setBankAccountNum(oo.createOmPoVendorsBankAccountNum(bd));
        coll.getOmPoVendors().add(obj);
        POVendorResponse response =pOVendorInsertion_Service.getPOVendorInsertion_pt().poVendorInsertionService(coll);
        String str =response.getPOVendorInsertionResponse();
        if(str.equals("Success")){
            MessageUtils.displayMessage(null, "Your changes have been saved successfully");
        }else{
            MessageUtils.displayErrorMessage(null, "Save not successful. There is some problem with the SOA service. Sorry for the inconvenience");
        }
        
        return null;
    }
}
